//Given a string s, determine if it is a palindrome, considering only alphanumer
//ic characters and ignoring cases. 
//
// 
// Example 1: 
//
// 
//Input: s = "A man, a plan, a canal: Panama"
//Output: true
//Explanation: "amanaplanacanalpanama" is a palindrome.
// 
//
// Example 2: 
//
// 
//Input: s = "race a car"
//Output: false
//Explanation: "raceacar" is not a palindrome.
// 
//
// 
// Constraints: 
//
// 
// 1 <= s.length <= 2 * 105 
// s consists only of printable ASCII characters. 
// 
// Related Topics 双指针 字符串 
// 👍 380 👎 0


package leetcode.editor.cn;

//Java：Valid Palindrome
class P125ValidPalindrome {
    public static void main(String[] args) {
        Solution solution = new P125ValidPalindrome().new Solution();
        // TO TEST
        System.out.println(solution.isPalindrome("8V8K;G;K;V;"));
    }

    //leetcode submit region begin(Prohibit modification and deletion)
    class Solution {
        public boolean isPalindrome(String s) {
            int left = 0, right = s.length() - 1;
            while (left < right) {
                char c = Character.toLowerCase(s.charAt(left));
                char ca = Character.toLowerCase(s.charAt(right));
                if ('a' <= c && c <= 'z' && 'a' <= ca && ca <= 'z') {
                    if (c == ca) {
                        left++;
                        right--;
                    } else {
                        return false;
                    }
                } else if ('0' <= c && c <= '9' && '0' <= ca && ca <= '9') {
                    if (c == ca) {
                        left++;
                        right--;
                    } else {
                        return false;
                    }
                } else if (('0' <= c && c <= '9' && 'a' <= ca && ca <= 'z') ||
                        ('0' <= ca && ca <= '9' && 'a' <= c && c <= 'z')) {
                    return false;
                } else if ('a' <= ca && ca <= 'z') {
                    left++;
                } else if ('a' <= c && c <= 'z') {
                    right--;
                } else if ('0' <= ca && ca <= '9') {
                    left++;
                } else if ('0' <= c && c <= '9') {
                    right--;
                } else {
                    left++;
                    right--;
                }


            }
            return true;
        }
        public boolean isPalindromeEarly(String s) {
            for (int i = 0, j = s.length() - 1; i <= j; i++, j--) {
                Character pre = s.charAt(i);
                char tail = s.charAt(j);
                if (Character.isLetterOrDigit(pre) && Character.isLetterOrDigit(tail)) {
                    if (Character.toLowerCase(tail) != Character.toLowerCase(pre)) {
                        return false;
                    }
                }
                if (!Character.isLetterOrDigit(pre)) {
                    j++;
                }
                if (!Character.isLetterOrDigit(tail)) {
                    i--;
                }
                if (i == j - 1) {
                    if (Character.isLetterOrDigit(pre) && Character.isLetterOrDigit(tail)) {
                        if (Character.toLowerCase(tail) != Character.toLowerCase(pre)) {
                            return true;
                        }
                    }
                }
            }

            return true;
        }
    }


//leetcode submit region end(Prohibit modification and deletion)

}